package com.xoa.controller.document;


import com.xoa.dao.document.DocumentMapper;
import com.xoa.model.document.DocumentModel;
import com.xoa.model.document.DocumentModelFlowRunPrcs;
import com.xoa.model.document.DocumentModelWithBLOBs;
import com.xoa.model.document.SysRuleModel;
import com.xoa.model.users.Users;
import com.xoa.service.document.DocumentSerivce;
import com.xoa.service.work.WorkService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.common.wrapper.BaseWrappers;
import com.xoa.util.dataSource.ContextHolder;
import com.xoa.util.page.PageParams;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

/**
 * 创建作者:   杨 胜
 * 创建日期:   2017-6-13 下午7:48:17
 * 类介绍  :    公文管理
 * 构造参数:   默认
 */
@Controller
@RequestMapping("/document")
@Scope("prototype")
public class DocumentController {


    @Resource
    DocumentSerivce documentSerivce;
    @Resource
    WorkService workService;
    @Resource
    DocumentMapper documentMapper;


    @RequestMapping("/mine")
    public String index(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/document/mine";
    }

    //骆鹏新建
    @RequestMapping("/newDocument")
    public String newDocument() {
        return "app/document/newDocument";
    }


    @RequestMapping("/pageTesting")//分页插件demo
    public String pageTesting() {
        return "app/officialDocument/pageTesting";
    }


    @RequestMapping("/makeADraft")//待办发文
    public String makeADraft() {
        return "app/officialDocument/dispatch/makeADraft";
    }

    @RequestMapping("/draftArticlesOfCommunication")//发文拟稿
    public String draftArticlesOfCommunication() {
        return "app/officialDocument/dispatch/draftArticlesOfCommunication";
    }

    @RequestMapping("/IthasBeenDone")//已办发文
    public String IthasBeenDone() {
        return "app/officialDocument/dispatch/IthasBeenDone";
    }

    @RequestMapping("/ISentAMessage")//我的发文
    public String ISentAMessage() {
        return "app/officialDocument/dispatch/ISentAMessage";
    }


    @RequestMapping("/addresseRegistrationForm")//收文登记
    public String addresseeRegistrationForm() {
        return "app/officialDocument/incomingDispatches/addresseeRegistrationForm";
    }

    @RequestMapping("/inAbeyance")//待办收文
    public String inAbeyance() {
        return "app/officialDocument/incomingDispatches/inAbeyance";
    }

    @RequestMapping("/myInReply")//我的收文
    public String myInReply() {
        return "app/officialDocument/incomingDispatches/myInReply";
    }

    @RequestMapping("/received")//已办收文
    public String received() {
        return "app/officialDocument/incomingDispatches/received";
    }

    @RequestMapping("/theOfficialDocumentQuery")//公文查询
    public String theOfficialDocumentQuery() {
        return "app/officialDocument/theOfficialDocumentQuery";
    }

    @RequestMapping("/documentsSupervisor")//公文公文督办
    public String documentsSupervisor() {
        return "app/officialDocument/documentsSupervisor";
    }

    @RequestMapping("/officialDocumentSet")//公文设置
    public String officialDocumentSet() {
        return "app/officialDocument/officialDocumentSet";
    }

    @RequestMapping("/theOfficialStatistics")//公文统计
    public String theOfficialStatistics() {
        return "app/officialDocument/theOfficialStatistics";
    }

    @RequestMapping("/receive")//来文接收
    public String netWorkReceive() {
        return "app/officialDocument/netWorkReceive";
    }

    @RequestMapping("/documentset")//公文设置页面
    public String officialExchange() {
        return "app/document/officialExchange";
    }

    @RequestMapping("/documentIndex")//公文门户页面
    public String documentIndex() {
        return "app/main/menu/documentIndex";
    }

    @RequestMapping("/documentDispatchReceive")//公文收发页面
    public String documentDispatchReceive() {
        return "app/officialDocument/documentDispatchReceive";
    }

    @RequestMapping("/marketIndex")//营销门户页面
    public String marketIndex() {
        return "app/main/menu/marketIndex";
    }

    @RequestMapping("/managementIndex")//管理门户页面
    public String managementIndex() { return "app/main/menu/managementIndex";
    }
    @RequestMapping("/humanResource")//人力资源门户页面
    public String humanResource() { return "app/main/menu/humanResource";
    }
    @RequestMapping("/reportFormIndex")//数据报表门户页面
    public String reportFormIndex() { return "app/main/menu/reportFormIndex";
    }
    @RequestMapping("/financialIndex")//财务门户页面
    public String financialIndex() { return "app/main/menu/financialIndex";
    }
    @RequestMapping("/companyPortal")//公司门户页面
    public String companyPortal() { return "app/main/menu/companyPortal";
    }
    @RequestMapping("/knowledgeIndex")//知识门户页面
    public String knowledgeIndex() { return "app/main/menu/knowledgeIndex";
    }
    @RequestMapping("/Institutional")//制度中心
    public String Institutional() { return "app/main/menu/Institutional";
    }
    @RequestMapping("/customMenu_ZDJ")//cs自定义门户
    public String customMenu_ZDJ() { return "app/main/customMenu/customMenu_ZDJ";
    }
    @RequestMapping("/personal_ZDJ")//cs自定义个人门户
    public String personal_ZDJ() { return "app/main/customMenu/personal_ZDJ";
    }
    @RequestMapping("/energyEfficiency_ZDJ")//cs自定义运营效能
    public String energyEfficiency_ZDJ() { return "app/main/customMenu/energyEfficiency_ZDJ";
    }
    @RequestMapping("/operationsCockpit_ZDJ")//cs自定义运营驾驶舱
    public String operationsCockpit_ZDJ() { return "app/main/customMenu/operationsCockpit_ZDJ";
    }
    @RequestMapping("/supervisionStatistics")// 督查情况统计映射
    public String supervisionStatistics() { return "app/supervise/supervisionStatistics/supervisionStatistics";
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-15 下午8:29:10
     * 方法介绍:   保存一条规则
     * 参数说明:   @param sysRule
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return ToJson<SysRuleModel>
     */
    @RequestMapping("/saveRule")
    @ResponseBody
    public ToJson<SysRuleModel> saveSysrule(SysRuleModel sysRule, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<SysRuleModel> tojson = new ToJson<SysRuleModel>();
        if (sysRule != null) {
            if (sysRule.getDigit() == null || "".equals(sysRule.getDigit())) {
                tojson.setMsg("保存失败，位数没有填写");
                tojson.setFlag(1);
                return tojson;
            }
            if (sysRule.getCode() == null || "".equals(sysRule.getCode())) {
                tojson.setMsg("保存失败，唯一标识没有填写");
                tojson.setFlag(1);
                return tojson;
            }
        }
        SysRuleModel ruleByCode = documentSerivce.getRuleByCode(sysRule);
        if (ruleByCode != null && ruleByCode.getCode() != null) {
            tojson.setMsg("保存失败，存在相同为唯一标识");
            tojson.setFlag(1);
        } else {
            int result = documentSerivce.saveSysRule(sysRule);
            if (result == 1) {
                tojson.setMsg("保存成功");
                tojson.setFlag(0);
            } else {
                tojson.setMsg("保存失败");
                tojson.setFlag(1);
            }
        }

        return tojson;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-15 下午8:29:37
     * 方法介绍:   删除一条
     * 参数说明:   @param sysRule
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return ToJson<SysRuleModel>
     */
    @RequestMapping("/deleteRule")
    @ResponseBody
    public ToJson<SysRuleModel> deleteRule(SysRuleModel sysRule, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<SysRuleModel> tojson = new ToJson<SysRuleModel>();
        int result = documentSerivce.deleteSysRule(sysRule);
        if (result == 1) {
            tojson.setMsg("删除成功");
            tojson.setFlag(0);
        } else {
            tojson.setMsg("删除失败");
            tojson.setFlag(1);
        }
        return tojson;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-15 下午8:30:11
     * 方法介绍:   编辑生成规则
     * 参数说明:   @param sysRule
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return ToJson<SysRuleModel>
     */
    @RequestMapping("/updateRule")
    @ResponseBody
    public ToJson<SysRuleModel> updateRule(SysRuleModel sysRule, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<SysRuleModel> tojson = new ToJson<SysRuleModel>();
        SysRuleModel ruleByCode = documentSerivce.getRuleByCode(sysRule);
        if (ruleByCode != null && !ruleByCode.getId().equals(sysRule.getId())) {
            tojson.setMsg("修改失败，存在相同为唯一标识");
            tojson.setFlag(1);
        } else {
            int result = documentSerivce.updateRuleById(sysRule);
            if (result == 1) {
                tojson.setMsg("修改成功");
                tojson.setFlag(0);
            } else {
                tojson.setMsg("修改失败");
                tojson.setFlag(1);
            }
        }
        return tojson;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-15 下午8:30:40
     * 方法介绍:   根据id获取一条规则信息
     * 参数说明:   @param sysRule
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return ToJson<SysRuleModel>
     */
    @RequestMapping("/getRuleById")
    @ResponseBody
    public ToJson<SysRuleModel> getRuleById(SysRuleModel sysRule, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<SysRuleModel> tojson = new ToJson<SysRuleModel>();
        SysRuleModel result = documentSerivce.getRuleById(sysRule, request);
        if (result != null) {
            tojson.setObject(result);
            tojson.setMsg("ok");
            tojson.setFlag(0);
        }
        return tojson;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-15 下午8:31:16
     * 方法介绍:   获取全部规则信息
     * 参数说明:   @param sysRule
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return ToJson<SysRuleModel>
     */
    @RequestMapping("/getRuleAll")
    @ResponseBody
    public ToJson<SysRuleModel> getRuleAll(SysRuleModel sysRule, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<SysRuleModel> tojson = new ToJson<SysRuleModel>();
        List<SysRuleModel> result = documentSerivce.getRuleAll(sysRule,request);

        if (result != null) {
            tojson.setFlag(0);
            tojson.setMsg("ok");
            tojson.setObj(result);
        } else {
            tojson.setFlag(1);
            tojson.setMsg("null");
            tojson.setObj(result);
        }

        return tojson;
    }


    @RequestMapping("/getRuleAll2")
    @ResponseBody
    public ToJson<SysRuleModel> getRuleAll2(SysRuleModel sysRule, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<SysRuleModel> tojson = new ToJson<SysRuleModel>();
        List<SysRuleModel> result = documentSerivce.getRuleAll2(sysRule,request);

        if (result != null) {
            tojson.setFlag(0);
            tojson.setMsg("ok");
            tojson.setObj(result);
        } else {
            tojson.setFlag(1);
            tojson.setMsg("null");
            tojson.setObj(result);
        }

        return tojson;
    }

    /**
     * @作者：张航宁
     * @时间：2017/8/22
     * @介绍：计数器+1
     * @参数：
     */
    @ResponseBody
    @RequestMapping("/updateCount")
    public ToJson<SysRuleModel> addCount(HttpServletRequest request, SysRuleModel sysRuleModel) {
        ToJson<SysRuleModel> json = new ToJson<SysRuleModel>();
        String count =request.getParameter("count");
        try {
            SysRuleModel ruleById = documentSerivce.getRuleByIds(sysRuleModel, request,Integer.parseInt(count));
            json.setObject(ruleById);
            json.setFlag(0);
            json.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    /**
     * 作者: 张航宁
     * 日期: 2017/9/21
     * 说明: 查看唯一标识是否被使用
     */
    @ResponseBody
    @RequestMapping("/codeIfUse")
    public ToJson<SysRuleModel> codeIfUse(SysRuleModel sysRuleModel) {
        ToJson<SysRuleModel> json = new ToJson<SysRuleModel>();
        try {
            SysRuleModel ruleByCode = documentSerivce.getRuleByCode(sysRuleModel);
            if (ruleByCode != null) {
                json.setFlag(1);
                json.setMsg("已被使用");
            } else {
                json.setFlag(0);
                json.setMsg("未被使用");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-21 下午2:45:59
     * 方法介绍:   保存一条公文信息
     * 参数说明:   @param document
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return ToJson<SysRuleModel>
     */
    @RequestMapping("/saveDoc")
    @ResponseBody
    public ToJson<Object> saveDocument(
            Integer fflowId,
            @RequestParam(value = "fflowStep", required = false) Integer fflowStep,
            @RequestParam(value = "fprcsId", required = false) String fprcsId,
            @RequestParam(value = "frunName", required = false) String frunName
            , DocumentModelWithBLOBs document, HttpServletRequest request) {
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
        String userId = (String) request.getSession().getAttribute("userId");
        Integer deptId = (Integer) request.getSession().getAttribute("deptId");
        document.setCurUserId(userId);
        document.setCreater(userId);
        document.setCreateDept(String.valueOf(deptId));
        ToJson<Object> result = documentSerivce.saveDocument(user, fflowId, fflowStep, fprcsId, frunName, document, request);
        return result;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-21 下午2:43:21
     * 方法介绍:   删除一条公文信息
     * 参数说明:   @param document
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return ToJson<SysRuleModel>
     */
    @RequestMapping("/deleteDoc")
    @ResponseBody
    public ToJson<DocumentModelWithBLOBs> deleteDocument(DocumentModelWithBLOBs document, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<DocumentModelWithBLOBs> tojson = new ToJson<DocumentModelWithBLOBs>();
        int result = documentSerivce.deleteDocument(document);
        if (result == 1) {
            tojson.setMsg("删除成功");
            tojson.setFlag(0);
        } else {
            tojson.setMsg("删除失败");
            tojson.setFlag(1);
        }
        return tojson;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-19 下午8:40:51
     * 方法介绍:   修改一条公文信息
     * 参数说明:   @param document
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return ToJson<SysRuleModel>
     */
    @RequestMapping("/updateDoc")
    @ResponseBody
    public ToJson<DocumentModelWithBLOBs> updateDocument(DocumentModelWithBLOBs document, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<DocumentModelWithBLOBs> tojson = new ToJson<DocumentModelWithBLOBs>();
        //将版式文件的后缀统一设置为小写   pdf
        if(!StringUtils.checkNull(document.getMainAipFileName())){
            String mainAipFileName = document.getMainAipFileName();
            String substring = mainAipFileName.substring(0, mainAipFileName.lastIndexOf("."))
                    +mainAipFileName.substring(mainAipFileName.lastIndexOf(".")).toLowerCase();
            document.setMainAipFileName(substring);
        }
        int result = documentSerivce.updateDocument(document);
        if (result == 1) {
            tojson.setMsg("修改成功");
            tojson.setFlag(0);
        } else {
            tojson.setMsg("修改失败");
            tojson.setFlag(1);
        }
        return tojson;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-19 下午8:41:24
     * 方法介绍:   通过id获取一条信息
     * 参数说明:   @param document
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return ToJson<SysRuleModel>
     */
    @RequestMapping("/selectDocById")
    @ResponseBody
    public ToJson<DocumentModelWithBLOBs> selectDocumentById(DocumentModelWithBLOBs document, HttpServletRequest request) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute("loginDateSouse"));
        ToJson<DocumentModelWithBLOBs> tojson = new ToJson<DocumentModelWithBLOBs>();
        try {
            DocumentModelWithBLOBs resultDocument = documentSerivce.selectDocumentById(request, document);
            tojson.setObject(resultDocument);
            tojson.setFlag(0);
            tojson.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
            tojson.setFlag(1);
            tojson.setMsg("err");
        }
        return tojson;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-19 下午8:41:24
     * 方法介绍:   公文查询
     * 参数说明:   @param document
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return ToJson<SysRuleModel>
     */
    @RequestMapping("/selectDocSelective")
    @ResponseBody
    public BaseWrappers selectDocSelective(
            Integer documentType,
            String title,
            Integer flowId,
            String dispatchType,
            String urgency,
            String secrecy,
            String userId,
            String deptId,
            String createTime,
            Integer runId,
            Integer status, PageParams pageParams,
            HttpServletRequest request) {
        return documentSerivce.selectDocSelective(documentType, title, flowId, dispatchType, urgency, secrecy, userId, deptId, createTime, status, pageParams, request, runId);
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-21 下午3:07:01
     * 方法介绍:   获取全部的信息
     * 参数说明:   @param document
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return ToJson<DocumentWithBLOBs>
     */
    @RequestMapping("/selectDocAll")
    @ResponseBody
    public ToJson<DocumentModelWithBLOBs> selectDocAll(DocumentModelWithBLOBs document, HttpServletRequest request) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute("loginDateSouse"));
        ToJson<DocumentModelWithBLOBs> tojson = new ToJson<DocumentModelWithBLOBs>();
        List<DocumentModelWithBLOBs> resultDocument = documentSerivce.selectDocAll(document);
        tojson.setObj(resultDocument);
        tojson.setFlag(0);
        return tojson;
    }

    @RequestMapping("/selectBacklogDoc")
    @ResponseBody
    public ToJson<DocumentModelWithBLOBs> selectBacklogDoc(HttpServletRequest request) {
        ToJson<DocumentModelWithBLOBs> tojson = new ToJson<DocumentModelWithBLOBs>();
        tojson.setFlag(0);
        return tojson;
    }

    @RequestMapping("/selectBacklogingDoc")
    @ResponseBody
    public ToJson<DocumentModelWithBLOBs> selectBacklogingDoc(HttpServletRequest request) {
        ToJson<DocumentModelWithBLOBs> tojson = new ToJson<DocumentModelWithBLOBs>();
        tojson.setFlag(0);
        return tojson;
    }

    @RequestMapping("/selectBacklogedDoc")
    @ResponseBody
    public ToJson<DocumentModelWithBLOBs> selectBacklogedDoc(HttpServletRequest request) {
        ToJson<DocumentModelWithBLOBs> tojson = new ToJson<DocumentModelWithBLOBs>();
        tojson.setFlag(0);
        return tojson;
    }

    @RequestMapping("/seleDocSendReceive")
    @ResponseBody
    public ToJson<DocumentModelWithBLOBs> seleDocSendReceive(String documentType, String curUserID, String allUserID, String dispatchType, String printDate,
                                                             HttpServletRequest request, @RequestParam("page") Integer page,
                                                             @RequestParam("pageSize") Integer pageSize,
                                                             @RequestParam("useFlag") Boolean useFlag) {
        return documentSerivce.seleDocSendReceive(documentType, curUserID, allUserID, dispatchType, printDate, request, page, pageSize, useFlag);


    }


    @RequestMapping("/selMyDocSendOrReceive")
    @ResponseBody
    public BaseWrappers selMyDocSendOrReceive(HttpServletRequest request, String documentType, String dispatchType, String printDate,
                                              String title, Integer docStatus,
                                              @RequestParam(name = "page", required = false, defaultValue = "0") Integer page,
                                              @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        return documentSerivce.selMyDocSendOrReceive(request, documentType, dispatchType, printDate, title, docStatus, page, pageSize);
    }

    @RequestMapping("/getRunName")
    @ResponseBody
    public BaseWrapper getRunName(HttpServletRequest request,
                                  Integer flowId,
                                  @RequestParam(value = "flowStep", required = false) Integer prcsId,
                                  @RequestParam(value = "prcsId", required = false) String flowProcs
    ) {
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
        return workService.getRunName(user, flowId, prcsId, flowProcs);

    }

    /**
     * 公文代办接口
     *
     * @param request
     * @param documentType 公文类型 0 发文 1 收文
     * @param dispatchType 发文类型
     * @param printDate    开始时间
     * @param page         页数 默认 0
     * @param pageSize     每页个数 默认10
     * @return
     */
    @RequestMapping("/selWillDocSendOrReceive")
    @ResponseBody
    public BaseWrappers selWillDocSendOrReceive(HttpServletRequest request, String documentType, String dispatchType, String printDate,String state,String beginTime,String endTime,
                                                String title, Integer docStatus, Integer runId,
                                                @RequestParam(name = "page", required = false, defaultValue = "0") Integer page,
                                                @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        return documentSerivce.selWillDocSendOrReceive(request, documentType, dispatchType, printDate, title, docStatus, page, pageSize, DocumentSerivce.PRCS_FLAG_WILL, state, beginTime, endTime);
    }

    /**
     * 公文已办接口（按步骤展开）
     *
     * @param request
     * @param documentType 公文类型 0 发文 1 收文
     * @param dispatchType 发文类型
     * @param printDate    开始时间
     * @param page         页数 默认 0
     * @param pageSize     每页个数 默认10
     * @return
     */
    @RequestMapping("/selOverDocSendOrReceive")
    @ResponseBody
    public BaseWrappers selOverDocSendOrReceive(HttpServletRequest request, String documentType, String dispatchType, String printDate,String state,String beginTime,String endTime,
                                                String title, Integer docStatus, Integer runId,
                                                @RequestParam(name = "page", required = false, defaultValue = "0") Integer page,
                                                @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        return documentSerivce.selWillDocSendOrReceive(request, documentType, dispatchType, printDate, title, docStatus, page, pageSize, DocumentSerivce.PRCS_FLAG_OVER, state, beginTime, endTime);
    }

    /**
     * 创建作者: 刘建
     * 创建日期: 2020-06-29 15:28
     * 方法介绍: 最新文件（公文 可传阅的 办结公文）
     * @param request
     * @param documentType
     * @param dispatchType
     * @param printDate
     * @param title
     * @param docStatus
     * @param page
     * @param pageSize
     * @return com.xoa.util.common.wrapper.BaseWrappers
     */
    @RequestMapping("/selMyDocument")
    @ResponseBody
    public BaseWrappers selMyDocument(HttpServletRequest request, String documentType, String dispatchType, String printDate,
                                      String title, Integer docStatus,
                                      @RequestParam(name = "page", required = false, defaultValue = "0") Integer page,
                                      @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        return documentSerivce.selMyDocument(request, documentType, dispatchType, printDate, title, docStatus, page, pageSize, DocumentSerivce.PRCS_FLAG_OVER);
    }


    /**
    *待办 或 已办  公文
     * @param willOrOver 待办0 已办1
     */
    @RequestMapping("/selWillDocOrOverDoc")
    @ResponseBody
    public BaseWrappers selWillDocOrOverDoc(HttpServletRequest request, String willOrOver, String documentType, String dispatchType,String state,String beginTime,String endTime,
                                            String printDate,String title, Integer docStatus, Integer runId,
                                            @RequestParam(name = "page", required = false, defaultValue = "0") Integer page,
                                            @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {

        if ("0".equals(willOrOver)){//待办
            documentSerivce.selWillDocSendOrReceive(request, documentType, dispatchType, printDate, title, docStatus, page, pageSize, DocumentSerivce.PRCS_FLAG_WILL, state, beginTime, endTime);
        }
        if ("1".equals(willOrOver)){//已办
            documentSerivce.selWillDocSendOrReceive(request, documentType, dispatchType, printDate, title, docStatus, page, pageSize, DocumentSerivce.PRCS_FLAG_OVER, state, beginTime, endTime);
        }
        return null;
    }

    @RequestMapping("/selOverDocSendOrReceiveOnly")
    @ResponseBody
    public BaseWrappers selOverDocSendOrReceiveNew(HttpServletRequest request, String documentType, String dispatchType, String printDate,
                                                   String title, Integer docStatus,
                                                   @RequestParam(name = "page", required = false, defaultValue = "0") Integer page,
                                                   @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        return documentSerivce.selOverDocSendOrReceive(request, documentType, dispatchType, printDate, title, docStatus, page, pageSize, DocumentSerivce.PRCS_FLAG_OVER);
    }

    @RequestMapping("/selOverseeTheOfficialDocument")
    @ResponseBody
    public BaseWrappers selOverseeTheOfficialDocument(HttpServletRequest request, String documentType, String dispatchType, String computationNumber,
                                                      @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                      @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                      @RequestParam(name = "useFlag", required = false, defaultValue = "true") Boolean useFlag, String printDate, String title

    ) {
        return documentSerivce.selOverseeTheOfficialDocument(request, documentType, dispatchType, computationNumber, page, pageSize, useFlag, printDate, title);
    }

    @RequestMapping("/addDocSendSms")
    @ResponseBody
    public BaseWrappers addDocSendSms(HttpServletRequest req, String cuName, String allName, String title, String classify, String flowId, String prcsId, String flowStep, String runId, String opFlag) {
        return documentSerivce.addDocSendSms(req, cuName, allName, title, classify, flowId, prcsId, flowStep, runId, opFlag);
    }

    @ResponseBody
    @RequestMapping("/getDocumentEditPriv")
    public ToJson<DocumentModel> getDocumentEditPriv(Integer id) {
        return documentSerivce.getDocumentEditPriv(id);
    }

    /**
     * @创建作者: 高亚峰
     * @创建日期: 2017/8/18 14:54
     * @函数介绍: 印章主界面映射地址
     * @参数说明: @param
     * @return:
     **/
    @RequestMapping("/SealIndex")
    public String SealIndex() {
        return "app/document/SealIndex";
    }

    /**
     * @创建作者: 高亚峰
     * @创建日期: 2017/8/18 14:54
     * @函数介绍: 印章新建映射地址
     * @参数说明: @param
     * @return:
     **/
    @RequestMapping("/SealNew")
    public String newSeal() {
        return "app/document/index";
    }

    /**
     * @创建作者: 高亚峰
     * @创建日期: 2017/8/18 14:54
     * @函数介绍: 印章设置权限地址
     * @参数说明: @param
     * @return:
     **/
    @RequestMapping("/SealSetting")
    public String SealSetting() {
        return "app/document/Sealsetting";
    }

    /**
     * @创建作者: 高亚峰
     * @创建日期: 2017/8/18 14:54
     * @函数介绍: 印章日志映射地址
     * @参数说明: @param
     * @return:
     **/
    @RequestMapping("/SealLog")
    public String SealLog() {
        return "app/document/SealLog";
    }

    @ResponseBody
    @RequestMapping("/selectTimeOutDoc")
    public BaseWrappers selectTimeOutDoc(HttpServletRequest request) {
        return documentSerivce.selectTimeOutDoc(request);
    }


    /**
     * @创建作者: 刘新婷
     * @创建日期: 2018-01-11
     * @函数介绍: 金格电子印章管理映射地址
     * @参数说明: @param
     * @return:
     **/
    @RequestMapping("/sealManagerIndex")
    public String sealManagerIndex() {
        return "app/document/sealManager";
    }

    //微端公文查询
    @ResponseBody
    @RequestMapping("/selDocByTitle")
    public BaseWrappers<DocumentModelFlowRunPrcs> selDocByTitle(HttpServletRequest request, String title, PageParams pageParams) {
        return documentSerivce.selDocByTitle(request, title, pageParams);
    }

    /**
     * 微应用页面映射
     *
     * @return
     */
    //发文管理页面
    @RequestMapping("/getDispah5")
    public String getDispah5() {
        return "app/officialDocument/m/dispah5";
    }

    //代办发文页面
    @RequestMapping("/getDispatchh5")
    public String getDispatchh5() {
        return "app/officialDocument/m/dispatchh5";
    }

    //已办发文页面
    @RequestMapping("/getOutgoingMessageh5")
    public String getOutgoingMessageh5() {
        return "app/officialDocument/m/outgoingMessageh5";
    }

    //收文管理页面
    @RequestMapping("/getReceiptMenth5")
    public String getReceiptMenth5() {
        return "app/officialDocument/m/receiptMenth5";
    }
    //代办收文页面

    @RequestMapping("/getAgencyReceipth5")
    public String getAgencyReceipth5() {
        return "app/officialDocument/m/agencyReceipth5";
    }

    //已办收文页面
    @RequestMapping("/getBeenOfficeh5")
    public String getBeenOfficeh5() {
        return "app/officialDocument/m/beenOfficeh5";
    }

    //上级来文页面
    @RequestMapping("/getSuperiorh5")
    public String getSuperiorh5() {
        return "app/officialDocument/m/superiorh5";
    }

    //上级来文详情页面
    @RequestMapping("/getSuperiorformh5")
    public String getSuperiorformh5() {
        return "app/officialDocument/m/superiorformh5";
    }

    //上级来文转发页面
    @RequestMapping("/getSuperturnh5")
    public String getSuperturnh5() {
        return "app/officialDocument/m/superturnh5";
    }

    //上级来文回退页面
    @RequestMapping("/getFeedbacks")
    public String getFeedbacks() {
        return "app/officialDocument/m/feedbacks";
    }

    //上级转交页面
    @RequestMapping("/zhuanjiaoh5")
    public String zhuanjiaoh5() {
        return "app/officialDocument/m/zhuanjiaoh5";
    }

    //公文查询页面
    @RequestMapping("/getDocumentInquiries")
    public String getDocumentInquiries() {
        return "app/officialDocument/m/documentInquiries";
    }

    //公文查询详情页面
    @RequestMapping("/getWorkformh5PreViews")
    public String getWorkformh5PreViews() {
        return "app/officialDocument/m/workformh5PreViews";
    }

    //我的收文列表
    @RequestMapping("/getMyinboxh5")
    public String getMyinboxh5() {
        return "app/officialDocument/m/myinboxh5";
    }

    //查看未用的文号
    @ResponseBody
    @RequestMapping("/unUseNo")
    public ToJson<SysRuleModel> unUseNo(HttpServletRequest request, SysRuleModel sysRuleModel,
                                        @RequestParam(name = "flowId", required = false, defaultValue = "0") Integer flowId) {
        SysRuleModel ruleById = documentSerivce.getRuleById(sysRuleModel, request);
        //Integer newCounter = ruleById.getCounter() + 1;
        //ruleById.setCounter(newCounter);
        return documentSerivce.unUseNo(request, ruleById, flowId);

    }
    //查看已用的文号
    @ResponseBody
    @RequestMapping("/UseNo")
    public ToJson<SysRuleModel> UseNo(HttpServletRequest request, @RequestParam(name = "flowId", required = false, defaultValue = "0") Integer flowId) {
        return documentSerivce.UseNo(request, flowId);
    }
    //查看flow_data_x表中是否存在此runId记录
    @ResponseBody
    @RequestMapping("/checkRunId")
    public ToJson<SysRuleModel> checkRunId(HttpServletRequest request,@RequestParam(name = "flowId", required = false, defaultValue = "0") Integer flowId,@RequestParam(name = "runId", required = false, defaultValue = "0") Integer runId) {
        return documentSerivce.checkRunId(request,flowId,runId);
    }
    //查看已用的文号
    @ResponseBody
    @RequestMapping("/findByRunId")
    public ToJson<DocumentModel> findByRunId(@RequestParam(name = "runId", required = false, defaultValue = "0") Integer runId) {
        ToJson<DocumentModel> json=new ToJson<>();
        DocumentModel documentModel = documentSerivce.checkRunId(runId);
        if(documentModel!=null){
            json.setObject(documentModel);
        }
        json.setFlag(0);
        return json;
    }
    //查询公文门户的统计数量
    @ResponseBody
    @RequestMapping("/selectCount")
    public ToJson selectCount(HttpServletRequest request){
        return  documentSerivce.selectCount(request);
    }
    //公文门户e-char图数据
    @ResponseBody
    @RequestMapping("/selectWillCount")
    public ToJson selectWillCount(HttpServletRequest request){
        return  documentSerivce.selectWillCount(request);
    }
    //公文门户柱状图数据
    @ResponseBody
    @RequestMapping("/selectBytypeCount")
    public ToJson selectBytypeCount(HttpServletRequest request){
        return  documentSerivce.selectBytypeCount(request);
    }
    //正文上传文件，如上传的pdf文件，将main_fie和main_file_name清空
    @ResponseBody
    @RequestMapping("/setMainFile")
    public ToJson setMainFile(DocumentModel documentModel){
        return  documentSerivce.setMainFile(documentModel.getId());
    }
    @ResponseBody
    @RequestMapping("/delPDF")
    public ToJson delPDF(Integer runId){
        ToJson json=new ToJson();
        int i = documentMapper.delPDF(runId);
        if (i>0){
            json.setFlag(0);
            json.setMsg("已清除PDF");
        }
        return json;
    }
    /**
     *cs办公门户公文管理数据获取
     * @param request
     * @param documentType 公文类型 0 发文 1 收文
     * @param dispatchType 发文类型
     * @param printDate    开始时间
     * @param page         页数 默认 0
     * @param pageSize     每页个数 默认10
     * @return
     */
    @RequestMapping("/getAssignDocData")
    @ResponseBody
    public BaseWrappers getAssignDocData(HttpServletRequest request, String documentType, String dispatchType, String printDate,
                                                String title, Integer docStatus, Integer runId,
                                                @RequestParam(name = "page", required = false, defaultValue = "0") Integer page,
                                                @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        return documentSerivce.getAssignDocData(request, documentType, dispatchType, printDate, title, docStatus, page, pageSize, DocumentSerivce.PRCS_FLAG_WILL);
    }

    /**
     *cs督办情况统计表
     * @param request
     * @param page         页数 默认 0
     * @param pageSize     每页个数 默认10
     * @return
     */
    @RequestMapping("/SupervisionStatistics")
    @ResponseBody
    public ToJson SupervisionStatistics(HttpServletRequest request, @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                        @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                        @RequestParam(name = "flowId", required = false, defaultValue = "362") Integer flowId,
                                        @RequestParam(name = "flag", required = false, defaultValue = "1") String flag) throws ParseException {
        return documentSerivce.SupervisionStatistics(request, page, pageSize,flowId,flag);
    }
}
